﻿using DragonSolution.Web.Common.Common;
using DragonSolution.Web.Data.Entity;
using DragonSolution.Web.Data.Enum;
using DragonSolution.Web.UI.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using static DragonSolution.Web.Common.Common.CommonUtil;

namespace DragonSolution.Web.UI.Areas.AdminMrg.Controllers
{
    [CheckLogin(true)]
    public class MNewsController : AdminBaseController
    {
        private int type= (int)Subject.新闻;
        private JavaScriptSerializer json = new JavaScriptSerializer();
        private MyDbContext db;
        public MNewsController()
        {
            db = base.GetContext();
        }        


        // GET: AdminMrg/MNews
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SelectType(int id) {
            ViewBag.cParentId = id;
            ViewBag.rootChkDisabled = true;
            return View();
        }

        /// <summary>
        /// 分类管理
        /// </summary>
        /// <returns></returns>
        public ActionResult TypeList() {
            return View();
        }

        /// <summary>
        /// 添加/编辑分类
        /// </summary>
        /// <returns></returns>
        public ActionResult AddType(int id) {
            //id=0表示添加
            if (id != 0) {
                Category c = db.Category.Find(id);
                if (c != null) {
                    ViewBag.cId = c.CategoryID;
                    ViewBag.cName = c.CategoryName;
                    ViewBag.cParentId = c.ParentID;
                }
            }
            return View();
        }

        public ActionResult AddNews() {
            return View();
        }

        [HttpPost]
        public ActionResult AddNews(string NewsName, bool isTop, string NewsPostTime,int NewsCategory, string NewsContent)
        {
            try
            {
                DateTime time = string.IsNullOrEmpty(NewsPostTime) ? DateTime.Now : Convert.ToDateTime(NewsPostTime);
                int is_top = isTop ? 1 : 0;
                NewsInfo news = new NewsInfo() { NewsCategory = NewsCategory, NewsName = NewsName, NewsContent = NewsContent, NewsPostTime = time, IsDelete = 0, IsTop = is_top, ViewNum = 0 };
                db.NewsInfo.Add(news);
                db.SaveChanges();
                ViewBag.msg = "添加成功";
            }
            catch (Exception)
            {
                ViewBag.msg = "添加失败";
            }
            return View();
        }

        public ActionResult EditNews(int id) {
            try {
                NewsInfo news = db.NewsInfo.Find(id);
                if (news != null)
                {
                    int typeId = news.NewsCategory;
                    Category c = db.Category.Find(typeId);
                    if (c != null) {
                        ViewBag.typeName = c.CategoryName;
                    }
                    return View(news);
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch (Exception)
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult EditNews(NewsInfo news)
        {
            int id = news.NewsID;
            NewsInfo n = db.NewsInfo.Find(id);//找到要更新的产品
            try
            {
                n.NewsName = news.NewsName;
                n.IsTop = news.IsTop;
                n.NewsCategory = news.NewsCategory;
                n.NewsContent = news.NewsContent;
                n.NewsPostTime = news.NewsPostTime;
                db.SaveChanges();
                int typeId = n.NewsCategory;
                Category c = db.Category.Find(typeId);
                if (c != null)
                {
                    ViewBag.typeName = c.CategoryName;
                }
                ViewBag.msg = "更新成功";
            }
            catch (Exception)
            {
                ViewBag.msg = "更新失败";
            }
            return View();
        }
    }
}